document.onkeydown = function( e ) 
{
    var KeyID = ( window.event ) ? event.keyCode : e.keyCode;
    var tankI = Math.round( tank.y / BLOCK_SIDE );
    var tankJ = Math.round( tank.x / BLOCK_SIDE );
    var lastDir = tank.direction;
    switch ( KeyID )
    {
        case LEFT_ARROW: 
        case keyA:
            tank.turn("left");
            if (//( tank.x - 1 <= level[tankI][tankJ - 1].x + BLOCK_SIDE ) &&
                ( level[tankI][tankJ].type == 0 ) &&
                ( level[tankI][tankJ - 1].type == 0 ) &&
                ( level[tankI - 1][tankJ - 1].type == 1 ) &&
                ( level[tankI + 1][tankJ - 1].type == 1 ) &&
                (( lastDir == "up" ) || ( lastDir == "down" )))
            {
                tank.x = level[tankI][tankJ].x;
                tank.y = level[tankI][tankJ].y + addRange;
            }
        break;
        case RIGHT_ARROW:
        case keyD:
            tank.turn("right");
            if (//( tank.x + tank.width + 1 >= level[tankI][tankJ + 1].x ) &&
                ( level[tankI][tankJ].type == 0 ) &&
                ( level[tankI][tankJ + 1].type == 0 ) &&
                ( level[tankI - 1][tankJ + 1].type == 1 ) &&
                ( level[tankI + 1][tankJ + 1].type == 1 ) &&
                (( lastDir == "up" ) || ( lastDir == "down" )))
            {
                tank.x = level[tankI][tankJ].x;
                tank.y = level[tankI][tankJ].y + addRange;
            }
        break;
        case UP_ARROW:
        case keyW:
            tank.turn("up");
            if (//( tank.y - 1 <= level[tankI - 1][tankJ].y + BLOCK_SIDE ) &&
                ( level[tankI][tankJ].type == 0 ) &&
                ( level[tankI - 1][tankJ].type == 0 ) &&
                ( level[tankI - 1][tankJ - 1].type == 1 ) &&
                ( level[tankI - 1][tankJ + 1].type == 1 ) &&
                (( lastDir == "left" ) || ( lastDir == "right" )))
            {
                tank.x = level[tankI][tankJ].x + addRange;
                tank.y = level[tankI][tankJ].y;
            }
        break;
        case DOWN_ARROW:
        case keyS:
            tank.turn("down");
            if (//( tank.y + tank.height + 1 >= level[tankI + 1][tankJ].y ) &&
                ( level[tankI][tankJ].type == 0 ) &&
                ( level[tankI + 1][tankJ].type == 0 ) &&
                ( level[tankI + 1][tankJ - 1].type == 1 ) &&
                ( level[tankI + 1][tankJ + 1].type == 1 ) &&
                (( lastDir == "left" ) || ( lastDir == "right" )))
            {
                tank.x = level[tankI][tankJ].x + addRange;
                tank.y = level[tankI][tankJ].y;
            }
        break;
    }
}

document.onkeyup = function( e ) 
{
    var KeyID = ( window.event ) ? event.keyCode : e.keyCode;
    switch ( KeyID )
    {
        case ESC:
            pause();
        break;
        case SPACE:
        case ENTER:
            tank.launchRocket();
        break;
        case LEFT_ARROW:
        case keyA:
            tank.dX = 0;
        break;
        case RIGHT_ARROW:
        case keyD:
            tank.dX = 0;
        break;
        case UP_ARROW:
        case keyW:
            tank.dY = 0;
        break;
        case DOWN_ARROW:
        case keyS:
            tank.dY = 0;
        break;
    }
} 